<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 文件管理
 */
class File_manager extends SDF_Controller {
    protected $file_root = 'Public';#文件模块操作的根路径，可以是相对或者绝对路径！
    protected $site_root = 'http://admin.sdf.pw/Public';#文件根路径相对网站的位置，用于返回文件下载地址和图片显示
    /**
     * 测试页面
     */
    public function test(){
        $this->load->view('admin/file_manager/test');
    }

    /**
     * 文件列表
     */
    public function index(){
        $source_dir = rtrim(realpath($this->file_root), DIRECTORY_SEPARATOR);
        $searct_keyword = $this->input->get_post('searct_keyword');
        if($searct_keyword){
            $searct_keyword  = str_replace(array('\\','/'),DIRECTORY_SEPARATOR,$searct_keyword);
            $searct_keyword  = trim($searct_keyword ,DIRECTORY_SEPARATOR);
            $glob_str = $source_dir.DIRECTORY_SEPARATOR.$searct_keyword;
        }else{
            $glob_str = $source_dir;
        }
        if(is_dir($glob_str)){
            $glob_str.=DIRECTORY_SEPARATOR.'*';
        }

        $files = glob($glob_str);
        $list = array();
        foreach($files as $filename){
            $list[] = $this->get_file_info($filename);
        }
        //print_r($list);
        $this->data['source_dir'] = $source_dir;
        $this->data['list'] = $list;
        $this->data['site_root'] = $this->site_root;
        $this->data['file_root'] = $this->file_root;
        $this->data['glob_str'] = $glob_str;
        $this->load->view($this->D.$this->C.'/'.$this->M,$this->data);
    }

    /**
     * 上传文件表单
     */
    public function upload_form(){
        $this->load->view($this->D.$this->C.'/'.$this->M,$this->data);
    }

    /**
     * 上传文件
     */
    public function upload(){

    }
    /**
     * 计算文件的大小
     * @param $size 文件大小（字节）
     * @return string 以友好的方式显示文件大小
     */
    protected function get_real_size($size){
        $kb = 1024; // Kilobyte
        $mb = 1024 * $kb; // Megabyte
        $gb = 1024 * $mb; // Gigabyte
        $tb = 1024 * $gb; // Terabyte
        if ($size < $kb) {
            return $size . " B";
        } else if ($size < $mb) {
            return round ( $size / $kb, 2 ) . " KB";
        } else if ($size < $gb) {
            return round ( $size / $mb, 2 ) . " MB";
        } else if ($size < $tb) {
            return round ( $size / $gb, 2 ) . " GB";
        } else {
            return round ( $size / $tb, 2 ) . " TB";
        }
    }
    /**
     * 获取某个文件信息
     * @param $file
     * @param array $returned_values
     * @return bool
     */
    protected function get_file_info($file, $returned_values = array('is_dir', 'server_path', 'size', 'date')){
        if ( ! file_exists($file)){
            return FALSE;
        }
        if (is_string($returned_values)){
            $returned_values = explode(',', $returned_values);
        }
        $fileinfo = pathinfo($file);
        foreach ($returned_values as $key) {
            switch ($key) {
                case 'is_dir':
                    $fileinfo['is_dir'] = is_dir($file);
                    break;
                case 'server_path':
                    $fileinfo['server_path'] = $file;
                    break;
                case 'size':
                    $fileinfo['size'] = $this->get_real_size(filesize($file));
                    break;
                case 'date':
                    $fileinfo['date'] = filemtime($file);
                    break;
                case 'readable':
                    $fileinfo['readable'] = is_readable($file);
                    break;
                case 'writable':
                    // There are known problems using is_weritable on IIS.  It may not be reliable - consider fileperms()
                    $fileinfo['writable'] = is_writable($file);
                    break;
                case 'executable':
                    $fileinfo['executable'] = is_executable($file);
                    break;
                case 'fileperms':
                    $fileinfo['fileperms'] = fileperms($file);
                    break;
            }
        }

        return $fileinfo;
    }
}